package pl.prv.polanie.client;

import java.util.ArrayList;
import java.util.List;

import pl.prv.polanie.client.dto.AcademicTitleDTO;
import pl.prv.polanie.client.model.Cache;
import pl.prv.polanie.client.widgets.schedule.custom.PreferenceTypeChooser;
import pl.prv.polanie.client.widgets.schedule.custom.SubjectEventEditionWidget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
import com.google.gwt.user.client.EventPreview;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class PomocniczeProby {
	
	/**
	 * Test sprawdzajacy ile mozna dodac EventPreview.
	 * 
	 */
	static EventPreview secondEP = null;
	
	public static void testEventPreview() {
		
		DOM.addEventPreview(new EventPreview() {
			public boolean onEventPreview(Event event) {
				if (DOM.eventGetType(event) == Event.ONCLICK) {
					GWT.log("EP1", null);
				} else if (DOM.eventGetType(event) == Event.ONMOUSEMOVE) {
					GWT.log("EP1", null);
				}
				return true;
			}
		});
		
		Button addSecondEPButton = new Button("add second EP");
		addSecondEPButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				
				secondEP = new EventPreview() {
					public boolean onEventPreview(Event event) {
						if (DOM.eventGetType(event) == Event.ONCLICK) {
							GWT.log("EP2", null);
						} else if (DOM.eventGetType(event) == Event.ONMOUSEMOVE) {
							GWT.log("EP2", null);
						}
						return true;
					}
				};
				
				DOM.addEventPreview(secondEP);
			}
		});
		

		Button removeSecondEPButton = new Button("remove second EP");
		removeSecondEPButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				DOM.removeEventPreview(secondEP);
			}
		});
		
		HorizontalPanel hp = new HorizontalPanel();
		hp.add(addSecondEPButton);
		hp.add(removeSecondEPButton);
		
		RootPanel.get().add(hp);
		
	}
	
	
	public static void testOnBrowserEvent() {
		
		AbsolutePanel outerAP = new AbsolutePanel(){
//			@Override
			public void onBrowserEvent(Event event) {
//				super.onBrowserEvent(event);
				Planner.log("OUT " + DOM.eventGetTypeString(event));
			}
		};
		outerAP.setStyleName("yellow");
		outerAP.setWidth("400px");
		outerAP.setHeight("400px");
		
		outerAP.sinkEvents(Event.MOUSEEVENTS);
		
//		DOM.setEventListener(outerAP.getElement(), new EventListener() {
//			public void onBrowserEvent(Event event) {
//				
//				Planner.log("OUT " + DOM.eventGetTypeString(event));
//				
//			}
//		});
//		
		
		
		Label innerAP = new Label("INNER RRRR") {
			public void onBrowserEvent(Event event) {
				super.onBrowserEvent(event);
				Planner.log("IN " + DOM.eventGetTypeString(event));
			}
		};
		innerAP.setStyleName("orange");
		innerAP.setWidth("100px");
		innerAP.setHeight("100px");
		
		
		
		outerAP.add(innerAP, 100, 100);
		
		
		
		RootPanel.get().add(outerAP, 100, 100);
		
		
//		DOM.setCapture(outerAP.getElement());
	}
	
	
	public static void testPreferenceTypeChooserWidget() {
		Cache.loadInitialData();
		Button b = new Button("ddodaj");
		
		b.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				
				final PreferenceTypeChooser ptc = new PreferenceTypeChooser(){
//					@Override
					protected void runOnSelectedItem() {
						super.runOnSelectedItem();
						Planner.log("PomocniczeProby.testPreferenceTypeChooserWidget() " + this.getSelectedItemDTO()); 
					}
				};
				RootPanel.get().add(ptc);
				
				
			}
		});		
		
		RootPanel.get().add(b);
		
	}
	

	
	private static EventPreview ffEP = new EventPreview() {
		public boolean onEventPreview(Event event) {
			
			switch (DOM.eventGetType(event)) {
			case Event.ONMOUSEUP:
				Planner.log(".onEventPreview: " + " UP!");
				DOM.removeEventPreview(ffEP);
				break;
			case Event.ONMOUSEMOVE:
				int left = DOM.eventGetClientX(event);
				int top = DOM.eventGetClientY(event);
				RootPanel.get().setWidgetPosition(html, left, top);
			default:
				break;
			}
			
			return true;
		}
	};
	
	private static HTML html = null;
	
	public static void testOnMouseInFF() {
		html = new HTML("testowy");
		Planner.disableSelection(html.getElement());
		html.addStyleName("test");
		RootPanel.get().add(html, 100, 100);
		
		
		html.addMouseListener(new MouseListenerAdapter() {
//			@Override
			public void onMouseDown(Widget sender, int x, int y) {
				super.onMouseDown(sender, x, y);
				DOM.addEventPreview(ffEP);
			}
		});
		
	}

	
	public static void testSEEditionWidget() {
		SubjectEventEditionWidget seew = new SubjectEventEditionWidget(null, null);
		seew.setText("Tips");
		seew.center();
		
	}
	
}
